package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/system/index")
@Api(tags = "首页")
public class IndexController extends BaseController {
    @Autowired
    private ITUserService tUserService;

    @Autowired
    private ITGroupService tGroupService;
    @Autowired
    private ITSensitivewordService sensitivewordService;
    @Autowired
    private ITRobotService robotService;
    @Autowired
    private ISysUserReportService userReportService;
    @Autowired
    private ITChannelService channelService;

    @ApiOperation(value = "首页信息")
    @GetMapping("/getInfo")
    public AjaxResult getInfo()
    {
        Map<String, Integer> map = new HashMap<>();
        // 获取今天00的时间戳
        // 当前日期
        LocalDate today = LocalDate.now();

        // 当天 00:00:00（本地时区）
        LocalDateTime startOfDay = today.atStartOfDay();

        // 转为时间戳（毫秒）
        long timestamp = startOfDay.atZone(ZoneId.systemDefault())
            .toInstant()
            .toEpochMilli();
        //平台总人数，今日注册用户，在线用户，群组数量，
        // 平台总人数
        int toDayCount = tUserService.getCount(0,timestamp,null);
        // 昨天平台总人数
        int yesterDayCount = tUserService.getCount(2,timestamp,null);
        // 昨天日期
        LocalDate yesterday = LocalDate.now().minusDays(1);

        // 昨天 00:00:00（本地时区）
        long timestampMillis = yesterday
            .atStartOfDay(ZoneId.systemDefault()) // 本地时区
            .toInstant()
            .toEpochMilli();
        // 今日注册用户
        int toDayRegisterCount = tUserService.getCount(3,timestamp,null);
        // 昨日注册用户
        int yesterDayRegisterCount = tUserService.getCount(4,timestamp,timestampMillis);
        //在线用户
        int onlineCount = tUserService.getSessionCount();
        //群组数量
        int groupCount = tGroupService.selectTGroupList(null).size();
        // 敏感词命中数
        // 机器人数量
        // 举报数量
        // 频道数量
        map.put("groupCount",groupCount);
        map.put("toDayCount",toDayCount);
        map.put("yesterDayCount",yesterDayCount);
        map.put("toDayRegisterCount",toDayRegisterCount);
        map.put("yesterDayRegisterCount",yesterDayRegisterCount);
        map.put("onlineCount",onlineCount);
        map.put("channelCount",channelService.getCount());
        map.put("reportCount",userReportService.getCount());
        map.put("robotCount", robotService.getCount());
        map.put("sensitiveWordHitCount",sensitivewordService.getCount());
        return success(map);
    }
}
